class Solution
{
public:
    vector<string> topKFrequent(vector<string> &words, int k)
    {
        unordered_map<string, int> wordCount;
        for (auto &word : words)
        {
            ++wordCount[word];
        }
        vector<string> uniqueWords;
        for (auto &[word, count] : wordCount)
        {
            uniqueWords.push_back(word);
        }
        sort(uniqueWords.begin(), uniqueWords.end(), [&](auto &s1, auto &s2)
             {
            if(wordCount[s1] == wordCount[s2])
            {
                return s1 < s2;
            }
            return wordCount[s1] > wordCount[s2]; });
        return {uniqueWords.begin(), uniqueWords.begin() + k};
    }
};